<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('关联主体类型')" prop="subjectType">
            <dictSelect
              v-model="form.subjectType"
              :clearable="true"
              dictionary="subject_type"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item :label="$t('关联主体名称')" prop="subjectName">
            <MainBodySelect
              v-model="form.subjectName"
              :parameters="form.subjectType"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item :label="$t('印章名称')" prop="electronicsName">
            <el-input v-model="form.electronicsName" :placeholder="$t('印章名称')" show-word-limit maxlength="32" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item :label="$t('印章管理人')" prop="ascription_admin">
            <UserSelect
              v-model="form.ascriptionAdmin"
              :placeholder="this.$t('选择用户')"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-radio-group v-model="radio">
            <el-radio :label="0">自动生成</el-radio>
            <el-radio :label="1">本地上传</el-radio>
            <FileTable :list.sync="form.fileList" data-type="deal_bf_valuation" :business-id="autoBusinessId($route.query.id, form)" :btn-show="!otherBtnHide" />
          </el-radio-group>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="oa:signature" />
    </div>
  </div>
</template>
<script>
import { saveOaElectronicSignature, getOaElectronicSignature } from '@/api/oa/signature/oaElectronicSignature'
import MainBodySelect from '@/components/MainBodySelect'
export default {
  components: {
    MainBodySelect
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 遮罩层
      loading: false,
      // 操作按钮默认不展示
      buttonShow: true,
      radio: 2,
      otherBtnHide: false
    }
  },
  created() {
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getOaElectronicSignature(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        electronicsName: undefined,
        subjectType: undefined,
        subjectName: undefined,
        ascriptionAdmin: undefined,
        remark: undefined,
        cover: undefined,
        coverPath: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function(type) {
      this.$refs['form'].validate(valid => {
        // 验证数据
        if (valid) {
          // 弹出遮罩层
          this.loading = true
          // 保存数据
          saveOaElectronicSignature(this.form).then(response => {
            if (response.code === 200) {
              // 点击的按钮是保存并启动流程
              if (type === 'process') {
                // 启动流程
                this.startProcess(response.data)
                // 点击的仅保存
              } else {
                this.msgSuccess(this.$t('保存成功'))
                // 关闭form页面
                this.closeForm()
              }
            } else {
              this.msgError(response.msg)
              this.loading = false
            }
          })
        } else {
          this.locationError(document)
        }
      })
    },
    // 启动流程
    startProcess: function(dataId) {
      this.loading = false
      // 启动流程时候传入的参数
      const processData = {}
      // 待办标题
      processData.title = this.$t('请根据需求设置待办标题')
      // 业务表名
      processData.dataTable = this.$t('业务表名')
      // 业务ID
      processData.dataId = dataId
      // 业务表单地址(待办页面需要打开的页面)
      processData.formPath = '@/views/url'
      // 菜单标识(根据菜单标识判断的走哪个流程)
      processData.menuKey = this.$t('菜单标识')
      this.startProcessByMenu(processData, () => {
        this.form.actStatus = 'D'
        this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
        this.closeForm()
      })
    }

  }

}
</script>
